/*
 * Copyright (C) 2003, Velocityme Partnership
 * ChangeDeltaItemBean.java
 *
 * Created on 28 July 2003, 09:02
 */

package com.velocityme.entity;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import com.velocityme.interfaces.*;
import com.velocityme.interfaces.ServiceUnavailableException;

/**
 *
 * @author  Robert Crida Work
 *
 * @ejb.bean
 *           type="CMP"
 *           cmp-version="2.x"
 *           name="ChangeDeltaItem"
 *           jndi-name="ejb/ChangeDeltaItem"
 *           view-type="local"
 * @ejb.transaction type="Required"
 *
 * @ejb.value-object match="*"
 *
 * @ejb.persistence table-name="changedeltaitem"
 *
 * @ejb.util generate="physical"
 *
 * @jboss.persistence create-table="true"
 *                    remove-table="false"
 */
public abstract class ChangeDeltaItemBean implements EntityBean {
    
    /**
    * Context set by container
    */
    protected javax.ejb.EntityContext m_entityContext;
   
    public void setEntityContext(javax.ejb.EntityContext entityContext) {
        m_entityContext = entityContext;
    }

    public void unsetEntityContext() {
        m_entityContext = null;
    }

    /** @ejb.create-method */
    public com.velocityme.interfaces.ChangeDeltaItemPK ejbCreate(com.velocityme.interfaces.ChangeDeltaLocal p_changeDeltaLocal,
                                                                 com.velocityme.interfaces.ChangeDeltaItemTypeLocal p_typeLocal,
                                                                 java.lang.String p_description) throws CreateException, EJBException {
        try {
            setChangeDeltaItemId(generateNewID());
        }
        catch(ServiceUnavailableException e) {
            throw new EJBException(e.getMessage());
        }
        // EJB 2.0 spec says return null for CMP ejbCreate methods.
        return null;
    }

    public void ejbPostCreate(com.velocityme.interfaces.ChangeDeltaLocal p_changeDeltaLocal,
                              com.velocityme.interfaces.ChangeDeltaItemTypeLocal p_typeLocal,
                              java.lang.String p_description) throws CreateException, java.rmi.RemoteException {
        // CMR relations must be set in ejbPostCreate
        setChangeDeltaLocal(p_changeDeltaLocal);
        setChangeDeltaItemTypeLocal(p_typeLocal);
        setDescription(p_description);
    }

    /**
     * Retrive a unique creation id to use for this bean.  This will end up
     * demarcating this bean from others when it is stored as a record
     * in the database.
     *
     * @return Returns an integer that can be used as a unique creation id.
     *
     * @throws ServiceUnavailableException Indicating that it was not possible
     *                                     to retrieve a new unqiue ID because
     *                                     the service is not available
     **/
    private Integer generateNewID() throws ServiceUnavailableException {
        int lUniqueId = -1;
        try {            
            SequenceGenerator lBean = SequenceGeneratorUtil.getHome().create();
            lUniqueId = lBean.getNextNumber("changedeltaitem","changeDeltaItemId");
            lBean.remove();
        }
        catch ( NamingException ne ) {
            throw new ServiceUnavailableException( "Naming lookup failure: " + ne.getMessage() );
        }
        catch ( CreateException ce ) {
            throw new ServiceUnavailableException( "Failure while creating a generator session bean: " + ce.getMessage() );
        }
        catch ( RemoveException re ) {
            // When the Bean cannot be removed after a while it will be taken back by the container
            // therefore ignore this exception
        }
        catch ( RemoteException rte ) {
            throw new ServiceUnavailableException( "Remote exception occured while accessing generator session bean: " +  rte.getMessage() );
        }
        
        return new Integer(lUniqueId);
    }
    
    /**
     *  This gets a local object from a remote object.
     *  @ejb.interface-method view-type="remote"
     */
    public com.velocityme.interfaces.ChangeDeltaItemLocal getLocal() {
        return (ChangeDeltaItemLocal)m_entityContext.getEJBLocalObject();
    }
    
    /** @ejb.interface-method view-type="local" */
    public abstract com.velocityme.valueobjects.ChangeDeltaItemValue getChangeDeltaItemValue();

    //=======================================================================
    //
    //                   ATTRIBUTES
    //
    //=======================================================================
      
    /**
     * @ejb.persistence column-name="changeDeltaItemId"
     * @ejb.interface-method view-type="local"
     * @ejb.pk-field
     */
    public abstract java.lang.Integer getChangeDeltaItemId();
    /** @ejb.interface-method view-type="local" */
    public abstract void setChangeDeltaItemId(java.lang.Integer changeDeltaItemId);

    /**
     * @ejb.persistence column-name="description"
     *                  jdbc-type="VARCHAR"
     *                  sql-type="${velocityme.sql.text.type}"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.lang.String getDescription();
    /** @ejb.interface-method view-type="local" */
    public abstract void setDescription(java.lang.String description);

    //=======================================================================
    //
    //                   RELATIONSHIPS
    //
    //=======================================================================
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="ChangeDeltaItem-ChangeDeltaItemType"
     *               role-name="ChangeDeltaItem-has-a-ChangeDeltaItemType"
     *               target-ejb="ChangeDeltaItemType"
     *               target-role-name="ChangeDeltaItemType-has-many-ChangeDeltaItems"
     *               target-multiple="yes"
     * @jboss.relation fk-column="changeDeltaItemTypeIdFk"
     *                 related-pk-field="changeDeltaItemTypeId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.ChangeDeltaItemTypeValue"
     *                   aggregate-name="ChangeDeltaItemTypeValue"
     */
    public abstract com.velocityme.interfaces.ChangeDeltaItemTypeLocal getChangeDeltaItemTypeLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setChangeDeltaItemTypeLocal(com.velocityme.interfaces.ChangeDeltaItemTypeLocal type);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="ChangeDeltaItem-ChangeDelta"
     *               role-name="ChangeDeltaItem-has-a-ChangeDelta"
     * @jboss.relation fk-column="changeDeltaIdFk"
     *                 related-pk-field="changeDeltaId"
     */
    public abstract com.velocityme.interfaces.ChangeDeltaLocal getChangeDeltaLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setChangeDeltaLocal(com.velocityme.interfaces.ChangeDeltaLocal changeDeltaLocal);
    
    //=======================================================================
    //
    //                   MANAGEMENT METHODS
    //
    //=======================================================================
        
}
